using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using NacosDecryptTool.Config;

namespace NacosDecryptTool
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Nacos Decrypt Tool");
            Console.WriteLine("请先确认appsettings.json中的配置信息");
            Console.WriteLine("-------------------------------------");
            
            var config = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("appsettings.json")
                .AddJsonFile("appsettings.dev.json", optional: true, reloadOnChange: true)
                .Build();

            var appSettings = config.Get<AppSettings>();

            if (string.IsNullOrEmpty(appSettings.Nacos.TargetNamespace))
            {
                Console.WriteLine("Usage: NacosDecryptTool <target_namespace>");
                Console.WriteLine("Or specify TargetNamespace in appsettings.json");
                return;
            }

            if (string.IsNullOrWhiteSpace(appSettings.SM4.IV) || string.IsNullOrWhiteSpace(appSettings.SM4.Key))
            {
                Console.WriteLine("Please specify SM4.IV and SM4.Key in appsettings.json");
                return;
            }

            try
            {
                var nacosHelper = new NacosHelper(appSettings);
                Console.WriteLine($"Starting migration from {appSettings.Nacos.SourceNamespace} to {appSettings.Nacos.TargetNamespace}");
                await nacosHelper.MigrateConfigsAsync();
                
                Console.WriteLine("Migration completed successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}